﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;
using WMSABL.SY;
using WMSAML.SY;
using ElectronicWMSA.Comm.Forms.Message;
using System.Windows.Forms;

namespace ElectronicWMSA.Comm.ClassFiles
{
    public static class SystemVariable
    {
        public static int User_ID;
        public static string Login_Name;
        public static string User_Name;
        public static int Menu_ID;
        public static string GetHostName()
        {
            return Dns.GetHostName();
        }
        public static List<string> UserRights = new List<string>();
        public static void Vaidation(int uid)
        {
            UserRights = null;
            DataSet ds1 = new DataSet();
            DataSet ds2 = new DataSet();
            DataSet ds3 = new DataSet();
            DataSet ds4 = new DataSet();
            List<string> listAll = new List<string>();
            WMSABL.PublicDataSet bll = new WMSABL.PublicDataSet();
            string permissionsStr = "";
            if (uid > 0)
            {
                ds1 = bll.GetDataSet("TUserRoleRelation", "User_id='" + uid + "'");
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    string roleid = "";
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        roleid = ds1.Tables[0].Rows[i]["Role_id"].ToString();
                        ds2 = bll.GetDataSet("TRolePermissionRelation", "Role_id='" + roleid + "'");
                        if (ds2.Tables[0].Rows.Count > 0)
                        {
                            permissionsStr += ds2.Tables[0].Rows[0]["Permission_id"].ToString();
                        }
                    }
                }
                ds1 = null;
                ds2 = null;
                ds3 = null;
                ds4 = null;
                ds1 = bll.GetDataSet("TUserGroupRelation", "User_id='" + uid + "'");
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    string groupid = "";
                    string roleid = "";
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        groupid = ds1.Tables[0].Rows[i]["Group_id"].ToString();

                        ds2 = bll.GetDataSet("TGroupPermissionRelation", "Group_id='" + groupid + "'");
                        if (ds2.Tables[0].Rows.Count > 0)
                        {
                            permissionsStr += ds2.Tables[0].Rows[0]["Permission_id"].ToString();
                        }

                        ds3 = bll.GetDataSet("TGroupRoleRelation", "Group_id='" + groupid + "'");
                        if (ds3.Tables[0].Rows.Count > 0)
                        {
                            for (int y = 0; y < ds3.Tables[0].Rows.Count; y++)
                            {
                                roleid = ds3.Tables[0].Rows[y]["Role_id"].ToString();

                                ds4 = bll.GetDataSet("TRolePermissionRelation", "Role_id='" + roleid + "'");
                                if (ds4.Tables[0].Rows.Count > 0)
                                {
                                    for (int h = 0; h < ds4.Tables[0].Rows.Count; h++)
                                    {
                                        permissionsStr += ds4.Tables[0].Rows[0]["Permission_id"].ToString();
                                    }
                                }
                            }
                        }
                    }
                }

                ds1 = null;
                ds1 = bll.GetDataSet("TUserPermissionRelation", "User_id='" + uid + "'");
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    permissionsStr += ds1.Tables[0].Rows[0]["Permission_id"].ToString();
                }
            }
            if (permissionsStr != "")
            {
                foreach (var item in permissionsStr.Substring(0, permissionsStr.Length - 1).Split(','))
                {
                    if (!listAll.Contains(item))
                    {
                        listAll.Add(item);
                    }
                }
                if (listAll.Count > 0)
                {
                    UserRights = listAll;
                }
            }
        }
        public static bool VaildationPermission(string type)
        {
            TPermissions_BLL bll_TPermissions = new TPermissions_BLL();
            TPermissions_Entity entity_TPermissions = new TPermissions_Entity();
            entity_TPermissions = bll_TPermissions.GetEntityByType(Menu_ID, type);

            if (entity_TPermissions == null || !UserRights.Contains(entity_TPermissions.ID.ToString()))
            {
                MessageClass.warning("无权限！");
                return false;
            }
            return true;
        }
        public static void AddLogInfo(string type, string opertation)
        {
            TSystemLogInfoBLL bll_loginInfo = new TSystemLogInfoBLL();
            TSystemLogInfoEntity entity_loginInfo = new TSystemLogInfoEntity();
            entity_loginInfo.UserID = User_ID;
            entity_loginInfo.UserName = User_Name;
            entity_loginInfo.Type = type;
            entity_loginInfo.OperateDateTime = DateTime.Now;
            entity_loginInfo.OperateDesc = opertation;
            entity_loginInfo.Status = "是";
            bll_loginInfo.Add_SystemLogInfo(entity_loginInfo);
        }
        public static void DataGridViewStyle(DataGridView dgv)
        {
            DataGridViewCellStyle headerStyle = new DataGridViewCellStyle();
            headerStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dgv.ColumnHeadersDefaultCellStyle = headerStyle;
            dgv.ClearSelection();
            dgv.CurrentCell = null;
            foreach (DataGridViewColumn item in dgv.Columns)
                item.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }
    }
}
